/*
 * @lc app=leetcode.cn id=1710 lang=javascript
 *
 * [1710] 卡车上的最大单元数
 */

// @lc code=start
/**
 * @param {number[][]} boxTypes
 * @param {number} truckSize
 * @return {number}
 */
var maximumUnits = function (boxTypes, truckSize) {
  let sum = 0;
  boxTypes.sort(([_1, size1], [_2, size2]) => size2 - size1);
  for (let i = 0; i < boxTypes.length; i++) {
    const [num, size] = boxTypes[i];
    if (truckSize > num) {
      sum += num * size;
      truckSize -= num;
    } else {
      sum += truckSize * size;
      break;
    }
  }
  return sum;
};
// @lc code=end

